/* eslint-disable no-unused-vars */
import namespace from "../../namespace";

/**
 * 绘制座椅基类
 */
class SeatPolicy extends namespace.policy.canvas.UniquePolicy {
  NAME = `${namespace.namespace}.policy.canvas.SeatPolicy`;

  constructor() {
    super();
    // 是否开始绘制座椅
    this.isStartDraw = false;

    this.startPoint = null;
    this.endPoint = null;

    // 起点座椅
    this.startSeat = null;

    // 动态绘制假座椅 此处应该替换为 扩展图形 -> 座椅图形类(组合图形)
    this.feakeSeat = null;
    this.seatDiameter = 20;
    this.seatSpace = 10; //座椅空隙 数据应该来自 座椅图形类

    this.seats = []; //存储动态假座椅信息

    // 总个数提示
    this.totalTextTip = null;

    // 矩形边界,用于计算text位置
    this.boundary = new namespace.geo.Rectangle(0, 0, 0, 0);
  }

  onKeyUp(canvas, keyCode) {
    keyCode === 27 && this.onPressEscKeyboard();
  }

  onMouseDown(canvas, x, y) {
    // 结束绘制
    if (this.isStartDraw) {
      this.finishDraw(x, y);
      return;
    }

    // 开始绘制
    this.startDraw(x, y);
  }

  // 动态绘制
  onMouseMove(canvas, x, y) {
    this.endPoint = new namespace.geo.Point(x, y);

    if (!this.isStartDraw) {
      this.createFeakeSeat().setCenter(x, y);
      return;
    }

    this.boundary.setBounds({
      x: this.startPoint.x,
      y: this.startPoint.y,
      w: x - this.startPoint.x,
      h: y - this.startPoint.y
    });

    // 动态绘制座椅
    this.drawCountText(this.drawFeakeSeats(x, y));
  }

  // 开始绘制
  startDraw(x, y) {
    this.isStartDraw = true;
    this.startPoint = new namespace.geo.Point(x, y);
    this.drawStartSeat(x, y);
  }

  // 绘制起始座椅
  drawStartSeat(x, y) {
    this.startSeat =
      this.startSeat ||
      new namespace.shape.SeatCircle({
        diameter: this.seatDiameter,
        x: 0,
        y: 0,
        stroke: 2
      });

    this.startSeat.hide = () => {
      this.startSeat.setCanvas(null);
    };

    this.startSeat.setCanvas(this.canvas).setCenter(x, y);
  }

  /**
   * 动态绘制
   * @param {*} x
   * @param {*} y
   * 返回 绘制个数
   */
  drawFeakeSeats(x, y) {
    return 0;
  }

  // 绘制总数文案
  drawCountText(total) {
    if (!this.totalTextTip) {
      this.totalTextTip = this.canvas.paper.text(x, y, "");
    }

    if (total < 3) {
      this.totalTextTip.attr({ text: "" });
      return;
    }

    let { x, y } = this.endPoint;
    this.totalTextTip.attr({ x: x + 20, y: y + 20, text: total });
  }

  // 完成绘制
  finishDraw(x, y) {
    this.isStartDraw = false;
    let start = this.startSeat.clone();
    this.canvas.addFigure(start);
    this.seats.forEach(s => {
      const clone = s.clone();
      clone.setAlpha(1);
      this.canvas.addFigure(clone);
    });

    this.totalTextTip && this.totalTextTip.remove();
    this.totalTextTip = null;

    this.startSeat && this.startSeat.hide();
  }

  // 计算座椅个数
  calcSeatTotla(distance) { }

  createFeakeSeat() {
    if (this.feakeSeat) {
      return this.feakeSeat;
    }

    this.feakeSeat = new namespace.shape.SeatCircle({
      diameter: this.seatDiameter,
      x: 0,
      y: 0,
      alpha: 0.5
    });

    this.feakeSeat.show = () => {
      this.feakeSeat.setCanvas(this.canvas);
    };

    this.feakeSeat.hide = () => {
      this.feakeSeat.setCanvas(null);
    };

    this.feakeSeat.show();

    return this.feakeSeat;
  }

  // 取消绘制
  onPressEscKeyboard() {
    this.isStartDraw = false;
    this.seats.forEach(shape => shape.setCanvas(null));
    this.startSeat && this.startSeat.hide();
    this.createFeakeSeat().setCenter(this.endPoint);
    this.totalTextTip && this.totalTextTip.remove();

    this.startSeat = null;
    this.totalTextTip = null;
  }

  onUninstall() {
    this.feakeSeat && this.feakeSeat.hide();
    this.feakeSeat = null;
    // 已经开始绘制
    this.isStartDraw && this.onPressEscKeyboard();
    super.onUninstall();
  }
}

namespace.policy.canvas.SeatPolicy = SeatPolicy;
